package com.ffs.template;

import com.ffs.template.out.FileConstant;
import com.ffs.template.out.FileOutFactory;
import com.ffs.template.out.IDataOut;
import com.ffs.template.table.TableAnalysis;
import com.ffs.template.table.TableAttr;
import com.ffs.template.util.ExecutorUtil;
import com.ffs.template.util.JVMClose;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

/**
 * 主程序类
 *
 * @author ffs80
 */
public class OutMain {

    private static final Options options = new Options();
    private static final String type = "type";

    static {
        options.addOption(type, true, "out type");
    }

    public static void main(String[] args) {

        try {
            CommandLine cmd = new DefaultParser().parse(options, args);
            if (cmd.hasOption(type)) {
                String optionValue = cmd.getOptionValue(type);
                List<Integer> typeList = getType(optionValue);
                if (typeList.isEmpty()) {
                    System.out.println("error input");
                    return;
                }

                //创建线程池
                ExecutorUtil.getInstance().setExecutorService(Executors.newFixedThreadPool(5));
                //加载数据库表属性
                Map<String, List<TableAttr>> tableMap = TableAnalysis.getInstance().dataLoad();

                for (Integer type : typeList) {
                    IDataOut handler = FileOutFactory.getHandler(type);
                    handler.write(tableMap, type);
                }
                ExecutorUtil.getInstance().shutDown();

            } else {
                System.out.println("not have args! Please input args!");
            }
            //程序关闭
            Runtime.getRuntime().addShutdownHook(new JVMClose());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    private static List<Integer> getType(String inputType) {
        List<Integer> outType = new ArrayList<>();
        switch (inputType) {
            case "entity":
                outType.add(FileConstant.FileType.ENTITY_TYPE);
                break;
            case "mapper":
                outType.add(FileConstant.FileType.MAPPER_TYPE);
                break;
            case "service":
                outType.add(FileConstant.FileType.ISERVICE_TYPE);
                outType.add(FileConstant.FileType.SERVICEIMPL_TYPE);
                break;
            case "iService":
                outType.add(FileConstant.FileType.ISERVICE_TYPE);
                break;
            case "serviceImpl":
                outType.add(FileConstant.FileType.SERVICEIMPL_TYPE);
                break;
            case "all":
                outType.add(FileConstant.FileType.ENTITY_TYPE);
                outType.add(FileConstant.FileType.MAPPER_TYPE);
                outType.add(FileConstant.FileType.ISERVICE_TYPE);
                outType.add(FileConstant.FileType.SERVICEIMPL_TYPE);
                break;
            default:

        }
        return outType;
    }
}
